<?php
	global $user;
	$username = $user->name;

    global $global_ws_address;

	$context = stream_context_create(array(
		'http' => array(
			'timeout' => 10      // Timeout in seconds
		)
	)); 
	
	$url1 = $global_ws_address.'o=getuserinfo&p1='.$username;
	$result1 = @file_get_contents($url1,0,$context);
	$result1 = json_decode($result1, true);

	if(is_array($result1['product'])){
		$userproducts ='';
		$productnum = count($result1['product']);
		for ($i=0; $i<$productnum; $i++){
			$userproducts .= $result1['product'][$i].';';
		}
		$url2 = $global_ws_address.'o=rankplist&p1='.$userproducts.'&p2=0';
		$result2 = @file_get_contents($url2,0,$context);
		$result2 = json_decode($result2, true);
	}

	if(is_array($result1['cve'])){
		$usercves ='';
		$cvenum = count($result1['cve']);
		for ($i=0; $i<$cvenum; $i++){
			$usercves .= $result1['cve'][$i].';';
		}
		$url3 = $global_ws_address.'o=ranklist&p1='.$usercves;
		$result3 = @file_get_contents($url3,0,$context);
		$result3 = json_decode($result3, true);
	}

	$url4 = $global_ws_address.'o=getnotification&p1='.$username.'&p2=0'; 
	$result4 = @file_get_contents($url4,0,$context);
	$result4 = json_decode($result4, true);

	if(!is_array($result1)){
		echo 'No results.';
	}else{
?>

<html>
<head>

	<script type="text/javascript" src="jquery.js"></script>
	
	<style type="text/css"> 
		#byProducts{
			margin:5px;
			padding:5px;
			background:#e5eecc;
			border:solid 2px #c3c3c3;
		}
		#byCVEs{
			margin:5px;
			padding:5px;
			background:#F5F5DC;
			border:solid 2px #c3c3c3;
		}
		#notification{
			margin:5px;
			padding:5px;
			background:#E6E6FA;
			border:solid 2px #c3c3c3;
		}
	</style>

</head>
 
<body>

	<p>
		This page gives you ranking of CVEs and notifications according to the information given in 'MY SETTINGS'.
	</p>

	<?php
		echo '<b>Username: '.$username.'</b><br/><br/>';
	?>

	<div id="byProducts">
		<fieldset>
			<legend>Your Top10 CVEs</legend>
<?php
			if(is_array($result2)){
				$result = $result2;
				$chart = array(
				  '#chart_id' => 'userproductscves',
				  '#title' => chart_title(t('Rank Result'), '0000ee', 15),
				  '#type' => CHART_TYPE_BAR_V,
				  '#size' => chart_size(700, 200),
				  '#bar_size' => chart_bar_size(40,10),
				);

				$i = 0;
				$x_axis1 = '';
				$x_axis2 = '|';
			  
				foreach( $result as $index => $value ){
				  $chart['#data'][$value['cvename']] = t($value['total']);
				  if( $i % 2 == 0 )  $x_axis1 .= $value['cvename'].'||';
				  else  $x_axis2 .= $value['cvename'].'||';
				  $i++;
				}

				$chart['#mixed_axis_labels'][CHART_AXIS_X_BOTTOM][0][] = chart_mixed_axis_label(t($x_axis1));
				if( $i > 1 )
				  $chart['#mixed_axis_labels'][CHART_AXIS_X_BOTTOM][1][] = chart_mixed_axis_label(t($x_axis2));

				$max = ceil($result[0]['total']/50) * 50;

				$chart['#mixed_axis_labels'][CHART_AXIS_Y_LEFT][0][] = chart_mixed_axis_range_label(0, $max);
				$chart['#data_scale'] = array('#min' => 0, '#max' => $max);

				$chart['#shape_markers'][] = array('N','000000',0,-1,11);

				echo chart_render($chart);

				echo '<br/>';
				echo '<ul style="text-align:left">';
				echo 'Click to see details.';
				foreach($result as $index => $value){
					echo '<li><a href="?q=node/14&cve='.$value['cvename'].'">'.$value['cvename'].'</a></li>';
				}
				echo '</ul>';
			}else{
				echo 'No results.';
			}
?>
		</fieldset>
	</div>

	<div id="byCVEs">
		<fieldset>
			<legend>Your CVEs Ranking</legend>
<?php
		if(is_array($result3)){
				$result = $result3;
				$chart = array(
				  '#chart_id' => 'usercveranking',
				  '#title' => chart_title(t('Rank Result'), '0000ee', 15),
				  '#type' => CHART_TYPE_BAR_V,
				  '#size' => chart_size(700, 200),
				  '#bar_size' => chart_bar_size(40,10),
				);

				$i = 0;
				$x_axis1 = '';
				$x_axis2 = '|';
			  
				foreach( $result as $index => $value ){
				  $chart['#data'][$value['cvename']] = t($value['total']);
				  if( $i % 2 == 0 )  $x_axis1 .= $value['cvename'].'||';
				  else  $x_axis2 .= $value['cvename'].'||';
				  $i++;
				}

				$chart['#mixed_axis_labels'][CHART_AXIS_X_BOTTOM][0][] = chart_mixed_axis_label(t($x_axis1));
				if( $i > 1 )
				  $chart['#mixed_axis_labels'][CHART_AXIS_X_BOTTOM][1][] = chart_mixed_axis_label(t($x_axis2));

				$max = ceil($result[0]['total']/50) * 50;

				$chart['#mixed_axis_labels'][CHART_AXIS_Y_LEFT][0][] = chart_mixed_axis_range_label(0, $max);
				$chart['#data_scale'] = array('#min' => 0, '#max' => $max);

				$chart['#shape_markers'][] = array('N','000000',0,-1,11);

				echo chart_render($chart);

				echo '<br/>';
				echo '<ul style="text-align:left">';
				echo 'Click to see details.';
				foreach($result as $index => $value){
					echo '<li><a href="?q=node/14&cve='.$value['cvename'].'">'.$value['cvename'].'</a></li>';
				}
				echo '</ul>';		
		}else{
			echo 'No results.';
		}
?>
		</fieldset>
	</div>

	<div id="notification">
		<fieldset>
			<legend>Notifications</legend>
<?php
		if(is_array($result4)){
			echo '<ul>';
			foreach($result4 as $index => $value){
				echo '<li>';
				echo $value['msg'].'	|	'.$value['date'];
				echo '</li>';
			}
			echo '</ul>';
			echo '<a> READ OLDER ENTRIES >> </a>';
		}else{
			echo 'No results.';
		}
?>			
		</fieldset>
	</div>

	
</body>
</html>
<?php
	}	
?>